//
//  FirstViewController.m
//  UI_作业05
//
//  Created by dllo on 16/6/8.
//  Copyright © 2016年 zuzhiming. All rights reserved.
//

#import "FirstViewController.h"
#import "CustomButton.h"
#import "CustomImageVIewAnimate.h"
#import "SecondViewController.h"
@interface FirstViewController ()

/** 自定义按钮属性 (六个按钮的样式对象)*/
@property (nonatomic, retain) CustomButton *customButton;

/** 按钮背景图片数据 */
@property (nonatomic, retain) NSMutableArray *imageArray;

/** 装载图片动画的相框 */
@property (nonatomic, retain) UIImageView *animatedImageView;


@end

@implementation FirstViewController

- (void)dealloc
{
    [_customButton release];
    [_imageArray release];
    [_animatedImageView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //加载背景图
    [self createBackGoundImageView];
    
    //加载按钮数据
    [self DataHandle];
    
    //加载自定义按钮
    [self createCustomButton];
    
    CustomButton *nextButton = [CustomButton buttonWithType:UIButtonTypeSystem Frame:CGRectMake(100, 50, 175, 40) BackGroundImage:nil];
    [nextButton setTitle:@"下一页" forState:UIControlStateNormal];
    nextButton.titleLabel.font = [UIFont systemFontOfSize:25.0f];
    [self.view addSubview:nextButton];
    
    [nextButton addTarget:self action:@selector(didClickedNextButton:) forControlEvents:UIControlEventTouchUpInside];

}

//创建背景图片
- (void)createBackGoundImageView
{
    //调取图片在工程中的路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"angry_0" ofType:@"jpg"];
    
    //将路径转化为图片对象
    UIImage *backGroundImage = [UIImage imageWithContentsOfFile:path];
    
    //图片载入相框
    self.animatedImageView = [[UIImageView alloc] initWithImage:backGroundImage];
    //设置相框的大小和屏幕一样大
    self.animatedImageView.frame = self.view.bounds;
    
    [self.view addSubview:_animatedImageView];
    [_animatedImageView release];
    
}

//图片数据处理
- (void)DataHandle
{
    self.imageArray = [NSMutableArray array];
    for (NSInteger i = 1; i <= 6; i++) {
        NSString *imageName = [NSString stringWithFormat:@"%ld.png", i];
        UIImage *image = [UIImage imageNamed:imageName];
        [self.imageArray addObject:image];
    }
}

//创建按钮
- (void)createCustomButton
{
    //六个按钮循环创建
    for (NSInteger i = 0; i < 2; i++) {
        for (NSInteger j = 0; j < 3; j++) {
            if (i == 0) {
                self.customButton = [CustomButton buttonWithType:UIButtonTypeCustom Frame:CGRectMake(10 + i * (375 - 20 - 40), 300 + j * (50 + 40), 40, 40) BackGroundImage:self.imageArray[j]];
                //利用循环的次数 递增tag值
                self.customButton.tag = 1000 + j;
                
                [self.view addSubview:_customButton];
                
                //设置按钮的点击事件
                [self.customButton addTarget:self action:@selector(didClickedCustomButton:) forControlEvents:UIControlEventTouchUpInside];
            }
            else
            {
                self.customButton = [CustomButton buttonWithType:UIButtonTypeCustom Frame:CGRectMake(10 + i * (375 - 20 - 40), 300 + j * (50 + 40), 40, 40) BackGroundImage:self.imageArray[j + 3]];
                
                //利用循环的次数 递增tag值
                self.customButton.tag = 1003 + j;
                
                [self.view addSubview:_customButton];
                
                //设置按钮的点击事件
                [self.customButton addTarget:self action:@selector(didClickedCustomButton:) forControlEvents:UIControlEventTouchUpInside];
            }
            
        }
    }
    
}


//点击方法 触发动画
- (void)didClickedCustomButton:(CustomButton *)button
{
    if (button.tag == 1000) {
        CustomImageVIewAnimate *imageViewCustom = [[CustomImageVIewAnimate alloc] initWithFrame:self.view.bounds ImageCount:26 ImageString:@"eat"];
        
        [self.animatedImageView addSubview:imageViewCustom];
        [imageViewCustom release];
        
        
    }
    if (button.tag == 1001) {
        CustomImageVIewAnimate *imageViewCustom = [[CustomImageVIewAnimate alloc] initWithFrame:self.view.bounds ImageCount:56 ImageString:@"scratch"];
        [self.animatedImageView addSubview:imageViewCustom];
        [imageViewCustom release];

    }
    if (button.tag == 1002) {
        CustomImageVIewAnimate *imageViewCustom = [[CustomImageVIewAnimate alloc] initWithFrame:self.view.bounds ImageCount:13 ImageString:@"cymbal"];
        [self.animatedImageView addSubview:imageViewCustom];
        [imageViewCustom release];

        
    }
    if (button.tag == 1003) {
        CustomImageVIewAnimate *imageViewCustom = [[CustomImageVIewAnimate alloc] initWithFrame:self.view.bounds ImageCount:28 ImageString:@"fart"];
        [self.animatedImageView addSubview:imageViewCustom];
        [imageViewCustom release];

    }
    if (button.tag == 1004) {
        CustomImageVIewAnimate *imageViewCustom = [[CustomImageVIewAnimate alloc] initWithFrame:self.view.bounds ImageCount:81 ImageString:@"drink"];
        [self.animatedImageView addSubview:imageViewCustom];
        [imageViewCustom release];

    }
    if (button.tag == 1005) {
        CustomImageVIewAnimate *imageViewCustom = [[CustomImageVIewAnimate alloc] initWithFrame:self.view.bounds ImageCount:24 ImageString:@"pie"];
        [self.animatedImageView addSubview:imageViewCustom];
        [imageViewCustom release];

    }
}

- (void)didClickedNextButton:(CustomButton *)button
{
    SecondViewController *svc = [[SecondViewController alloc] init];
    [self presentViewController:svc animated:YES completion:nil];
    [svc release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    
}



@end
